package cn.livingCloud.entity.core;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.context.Config;
import cn.livingCloud.entity.system.Setting;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.entity.user.User;
import cn.livingCloud.repository.*;
import cn.livingCloud.service.TemplateInfo;
import cn.livingCloud.utils.UuidGenerator;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

/**
 * 案件留言
 */
public class CaseComments extends BaseEntity implements TemplateInfo {
    private static final String URL_TEMPLATE = Config.getDomainUrl() + "front/cmc/case/info.htm?uuid=%s";

    private transient MemberMapper memberMapper = BeanProvider.getBean(MemberMapper.class);
    private transient EmployeeMapper employeeMapper = BeanProvider.getBean(EmployeeMapper.class);
    private transient UserMapper userMapper = BeanProvider.getBean(UserMapper.class);
    private transient CaseMapper caseMapper = BeanProvider.getBean(CaseMapper.class);
    private transient SettingMapper settingMapper = BeanProvider.getBean(SettingMapper.class);

    private String uuid = UuidGenerator.generate();

    private String caseUuid;

    private Boolean disable = false;

    private String content;

    /**
     * 留言的平台员工
     */
    private String userUuid;

    /**
     * 回复员工
     */
    private String employeeUuid;

    /**
     * 留言的用户
     */
    private String memberUuid;

    public CaseComments() {
    }

    public String getUuid() {
        return uuid;
    }

    public CaseComments setUuid(String uuid) {
        this.uuid = uuid;
        return this;
    }

    public String getCaseUuid() {
        return caseUuid;
    }

    public CaseComments setCaseUuid(String caseUuid) {
        this.caseUuid = caseUuid;
        return this;
    }

    @Override
    public Date getCreateDateTime() {
        return createDateTime;
    }

    @Override
    public CaseComments setCreateDateTime(Date createDateTime) {
        this.createDateTime = createDateTime;
        return this;
    }

    @Override
    public Boolean getArchive() {
        return archive;
    }

    @Override
    public CaseComments setArchive(Boolean archive) {
        this.archive = archive;
        return this;
    }

    public Boolean getDisable() {
        return disable;
    }

    public CaseComments setDisable(Boolean disable) {
        this.disable = disable;
        return this;
    }

    public String getContent() {
        return content;
    }

    public CaseComments setContent(String content) {
        this.content = content;
        return this;
    }

    public String getUserUuid() {
        return userUuid;
    }

    public CaseComments setUserUuid(String userUuid) {
        this.userUuid = userUuid;
        return this;
    }

    public String getEmployeeUuid() {
        return employeeUuid;
    }

    public CaseComments setEmployeeUuid(String employeeUuid) {
        this.employeeUuid = employeeUuid;
        return this;
    }

    public String getMemberUuid() {
        return memberUuid;
    }

    public CaseComments setMemberUuid(String memberUuid) {
        this.memberUuid = memberUuid;
        return this;
    }

    public Member getMember() {
        return memberMapper.findMemberByUuid(this.memberUuid);
    }

    public Employee getEmployee() {
        return employeeMapper.selectByPrimaryKey(this.employeeUuid);
    }

    public User getUser() {
        return userMapper.findUserByUuid(this.userUuid);
    }

    @Override
    public String getTemplateId() {
        final Setting setting = settingMapper.findSetting();
        if (setting == null || StringUtils.isEmpty(setting.getTemplateId())) {
            return "";
        }
        return setting.getTemplateId();
    }

    @Override
    public String getOpenid() {
        Case aCase = caseMapper.selectByPrimaryKey(this.caseUuid);
        Member member = aCase.getMember();
        return member.getOpenid();
    }

    @Override
    public String getUrl() {
        Case aCase = caseMapper.selectByPrimaryKey(this.caseUuid);
        return String.format(URL_TEMPLATE, aCase.getUuid());
    }

    @Override
    public Object getData() {
        return this;
    }

    @Override
    public String getTemplateContent() {
        final Setting setting = settingMapper.findSetting();
        if (setting == null || StringUtils.isEmpty(setting.getTemplateId())) {
            return "";
        }
        return setting.getTemplateContent();
    }
}